# URL = 'http://fanyi.baidu.com'
import re
import execjs
import requests

index_url = 'https://fanyi.baidu.com/'
lang_url = 'https://fanyi.baidu.com/langdetect'
translate_api = 'https://fanyi.baidu.com/v2transapi'
headers = {
    'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7',
    'Accept-Language': 'zh-CN,zh;q=0.9',
    'Cache-Control': 'no-cache',
    'Connection': 'keep-alive',
    'Pragma': 'no-cache',
    'Sec-Fetch-Dest': 'document',
    'Sec-Fetch-Mode': 'navigate',
    'Sec-Fetch-Site': 'same-origin',
    'Sec-Fetch-User': '?1',
    'Upgrade-Insecure-Requests': '1',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.6261.95 Safari/537.36',
    'sec-ch-ua': '"Chromium";v="122", "Not(A:Brand";v="24", "Google Chrome";v="122"',
    'sec-ch-ua-mobile': '?0',
    'sec-ch-ua-platform': '"Windows"',
}
cookies = {
    'smallFlowVersion': 'old',
    'REALTIME_TRANS_SWITCH': '1',
    'FANYI_WORD_SWITCH': '1',
    'HISTORY_SWITCH': '1',
    'SOUND_SPD_SWITCH': '1',
    'SOUND_PREFER_SWITCH': '1',
    'PSTM': '1724597722',
    'BIDUPSID': 'CDF765CCEBBB96297ECC489CD14BEE7F',
    'BAIDUID': 'EE1A4B2DC8CABDCDDB0741BB5B6E373E:FG=1',
    'H_WISE_SIDS': '110085_299594_603326_298696_301025_307086_607111_277936_611625_612162_613053_613278_613587_614859_615384_615451_615461_615311_615939_614395_616198_616160_616310_616318_616433_614437_614837_309636_615165_617019_617224_617430_608005_617507_617509_615676_614595_617312_617590_616120_612886_617651_614425_607028_617756_615838_616565_617752_617108_617818_617671_282466_618016_618032_617961_618094_618202_618224_618218_618208_618212_618314_618346_618455_618438_618445_618452_618463_618428_618470_618468_618477_618529_614026_618115_618643_618638_618655_618666_618685_618678_618609_618593_618619_618672_618599_618630_618585_618699_618480_613673_617842_618232_618195_613117',
    'H_PS_PSSID': '61027_61241_61360_61366_61390_61392_61426_61435_60853_61429_61509_61519',
    'BAIDUID_BFESS': 'EE1A4B2DC8CABDCDDB0741BB5B6E373E:FG=1',
    'ZFY': '2VdTKVwFCBR1mqoCpmRJkbCT0qpK7oGa0S0E0kMMjOA:C',
    'Hm_lvt_64ecd82404c51e03dc91cb9e8c025574': '1739353041',
    'Hm_lpvt_64ecd82404c51e03dc91cb9e8c025574': '1739353041',
    'HMACCOUNT': 'A93D1D4ED57DF201',
    'ab_sr': '1.0.1_MGQ2MmZhN2U4ODc5NzFiNjIyMzRhYTQ0YTkxMmU4ZjY4ZDk4NmMwODk5YjYyZWFkYTE3NzAxOTUzNTQyMWU2NDgwN2Y0NzFiNGRjODNmY2MwZGU3OWRiYmM3Y2RjYjdkZDkzMTkyY2E0MzVmZmNjY2UwYTkwMThmMTdjODZkNzM4OTgzOGFiYmRlMzE3NDllN2NiNzhkMTZmZWFiNjJlZTM3Y2Q3OGQ3YzBiYWNmMmQ0OTM4OWRjZjVmZTJiYTBkN2JiNTAwZTRmOGFjNzEzZmQ4OWFhM2IyNWY3MWIyYjc=',
}


def get_token():
    # 必须携带百度CK才能获取正常的响应
    response = requests.get(index_url, cookies=cookies, headers=headers)
    token = re.search(r"token: '(.*?)'", response.text).group(1)
    return token


def get_sign(query):
    with open('baidu_fanyi.js', 'r', encoding='utf-8') as f:
        baidu_js = f.read()
    sign = execjs.compile(baidu_js).call('get_sign', query)
    return sign


def get_result(lang, query, sign, token):
    data = {
        'from': lang,
        'to': 'en',
        'query': query,
        'transtype': 'realtime',
        'simple_means_flag': '3',
        'sign': sign,
        'token': token,
    }
    response = requests.post(url=translate_api, headers=headers, data=data, cookies=cookies)
    result = response.json()['trans_result']['data'][0]['dst']
    return result


def main():
    query = input('请输入要翻译的文字：')
    response = requests.post(url=lang_url, headers=headers, data={'query': query})
    lang = response.json()['lan']
    token = get_token()
    sign = get_sign(query)
    result = get_result(lang, query, sign, token)
    print('翻译成英文的结果为：', result)


if __name__ == '__main__':
    main()
